import scrapy


class ImgSpider(scrapy.Spider):
    name = "img"
    # allowed_domains = ["netbian.com"]
    start_urls = ["http://www.netbian.com/"]

    def parse(self, response, **kwargs):
        # 获取到详情页的url
        detail_href_list = response.xpath('//div[@class="list"]/ul/li/a/@href').extract()
        for url in detail_href_list:
            # 获取到完整的url
            full_url = response.urljoin(url)
            # print(full_url)
            # 对详情页发起请求
            yield scrapy.Request(url=full_url, callback=self.parse_detail)

    # 对详情页面的处理
    def parse_detail(self, response, **kwargs):
        # 获取大图详情页的url地址
        big_img_detail = response.xpath('//div[@class="photops"]/a/@href').extract()
        full_bigimg_url = response.urljoin(big_img_detail[0])
        # print(full_bigimg_url)
        # 对详情页发起请求
        yield scrapy.Request(url=full_bigimg_url, callback=self.parse_bigimg_detail)

    def parse_bigimg_detail(self, response, **kwargs):
        src = response.xpath('//td[@align="left"]/a/img/@src').extract_first()
        # print("src: ", src)
        yield {'src': src}
